package com.system.weirdor.facade.provider.impl;

import com.system.weirdor.facade.provider.RoleFacadeApi;
import com.system.weirdor.module.mapper.ResourceMapper;
import com.system.weirdor.module.mapper.RoleMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by weirdor on 2017/8/24.
 */
@Service
public class RoleFacadeApiImpl implements RoleFacadeApi {

@Autowired
private  RoleMapper roleMapper;

@Autowired
private ResourceMapper resourceMapper;

    public Set<String> listUserPerms(Long userId) {
        List<String> perms = resourceMapper.listUserPerms(userId);
        Set<String> permsSet = new HashSet<>();
        for(String perm : perms) {
            if(StringUtils.isNotBlank(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

    public Set<String> listUserRoles(Long userId) {
        List<String> roles = roleMapper.listUserRoles(userId);
        Set<String> rolesSet = new HashSet<>();
        for(String role : roles) {
            if(StringUtils.isNotBlank(role)) {
                rolesSet.addAll(Arrays.asList(role.trim().split(",")));
            }
        }
        return rolesSet;
    }



}